package com.killuasy.tools.servlet.magazine;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Properties;
//import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.killuasy.utils.io.OutPutFile;
import com.killuasy.utils.SysInit;


public class WwwMadouerComMain {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		// get url
		Properties props = new Properties();
		try {
			props.load(new InputStreamReader(new FileInputStream(
					"WwwMadouerComConfig.properties"), SysInit.propEncoding));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		String url = (String) props.get("url");
		String rootURL = (String) props.get("rootURL");
		String subRootURL = url.substring(0, url.lastIndexOf("/") + 1);
		String encoding = (String) props.get("encoding");
		String nextStr = (String) props.get("nextStr");
		String contentPStr = (String) props.get("contentPStr");
		String outPutFileName = (String) props.get("outPutFileName");

		Pattern p = Pattern.compile(nextStr);
		String nextPageStr = null;
		String[] result = null;

		ArrayList<String> imgStrList = null;

		Pattern contentP = Pattern.compile(contentPStr);
		Pattern urlPattern = Pattern.compile("'");
		
		ArrayList<String> imgURLList = new ArrayList<String>();
		
		System.out.println("Processing URL Start.");
		
		do {
			System.out.println("Processing URL:"+url);
			// get current Page Content
//			imgStrList = WebUtil.readPageSource(url, encoding, contentP, true);
			for(String imgStr:imgStrList){
				result = urlPattern.split(imgStr);
				imgURLList.add(rootURL+result[1]);
			}
			// get next URL

//			nextPageStr = WebUtil.readPageSource(url, encoding, p, false).get(0);
			if (nextPageStr != null && !"".equals(nextPageStr)) {
				result = urlPattern.split(nextPageStr);
				url = subRootURL + result[1];
			} else {
				 break;
			}

		} while (!"#".equals(result[1]));
		
		System.out.println("Processing URL End.");
		System.out.println("=================================");
		System.out.println("OutFile Start.");
		OutPutFile.outPutLinkFile(outPutFileName+".htm", imgURLList);
		System.out.println("OutFile End.");
		
		
//		String contentPStr = (String) props.get("contentPStr");
//		Pattern contentP = Pattern.compile(contentPStr);
//		Matcher m = contentP.matcher("                  <img src='/uploads/allimg/2011-06/15222937-1-31032.jpg' id='bigimg'  alt='健康之友womens day杂志2011年6月' border='0' />");
//		System.out.println(m.find());
	}	

}
